<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="公告编号" prop="noticeNum">
    <el-input v-model="ruleForm.noticeNum"></el-input>
  </el-form-item>
  <el-form-item label="公告标题" prop="noticeTitle">
    <el-input v-model="ruleForm.noticeTitle"></el-input>
  </el-form-item>
  <el-form-item label="公告内容" prop="noticeContent">
    <el-input v-model="ruleForm.noticeContent"></el-input>
  </el-form-item>
   
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          noticeNum: '',
          noticeTitle: '',
          noticeContent: '',
        },
        rules: {
          noticeNum: [
            { required: true, message: '请输入公告编号', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
           noticeTitle: [
            { required: true, message: '请输入公告标题', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          noticeContent: [
            { required: true, message: '请输入公告内容', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
        
        }
      };
    },
    methods: {
      submitForm(formName) {
          const _this=this
          var qs = require('qs');
        this.$refs[formName].validate((valid) => {
          if (valid) {
               axios({
            method:"post",
            url:"http://39.99.160.25:8088/notices/web/add_notice",
            data:qs.stringify(this.ruleForm),
            headers:
            {
              // "Authorization":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ7XCJpZFwiOjksXCJ1c2VybmFtZVwiOm51bGwsXCJwaG9uZVwiOm51bGwsXCJhZGRyZXNzXCI6bnVsbCxcInJlbWFya1wiOm51bGwsXCJhdXRob3JpdHlcIjowLFwicGFzc3dvcmRcIjpcIjExMTExMVwiLFwiaGVhZF9wb3J0cmFpdFwiOm51bGwsXCJhcHBpZFwiOm51bGwsXCJzY29wZVwiOm51bGwsXCJzdGF0ZVwiOm51bGwsXCJ3aGV0aGVyX2RlbGV0ZVwiOm51bGwsXCJpZGVudGl0eV9pZFwiOlwiMTExMTExXCIsXCJjcmVhdGVfdGltZVwiOm51bGwsXCJ1cGRhdGVfdGltZVwiOm51bGwsXCJkZWxldGVfdGltZVwiOm51bGwsXCJvcmRlcnNMaXN0XCI6bnVsbH0iLCJleHAiOjE2MDUwOTU5OTZ9.TBEM_7Ti_Mm_UB9vD236lPstsqMjWAawBaBDFSp6oN0"
            'Authorization':sessionStorage.getItem('token'),
            }
            
        }).then(function (resp){
              if(resp.data.data.msg=='通知添加成功'){
                _this.$alert('《'+_this.ruleForm.noticeNum+'》公告添加成功！', '消息', {
          confirmButtonText: '确定',
          callback: action => {
               _this.$router.push('/notices')
          }  
        });
            }else{
               _this.$alert('《'+_this.ruleForm.noticeNum+'》公告添加失败！', '消息', {
          confirmButtonText: '确定',
        });
          }
            
        })
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    
  }
</script>